package org.jeecg.modules.icon.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.config.service.ISysConfigService;
import org.jeecg.modules.icon.check.ParamCheckUtils;
import org.jeecg.modules.icon.entity.BimGeoIcon;
import org.jeecg.modules.icon.service.IBimGeoIconService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: bim_geo_icon
 * @Author: jeecg-boot
 * @Date: 2022-12-06
 * @Version: V1.0
 */
@Api(tags = "Icon配置")
@RestController
@RequestMapping("/geo/bimGeoIcon")
@Slf4j
public class BimGeoIconController extends JeecgController<BimGeoIcon, IBimGeoIconService> {

    @Autowired
    private IBimGeoIconService bimGeoIconService;

    @Autowired
    private ISysConfigService sysConfigService;
    /**
     * 分页列表查询
     *
     * @param bimGeoIcon
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "Icon分页列表查询", notes = "Icon分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<BimGeoIcon>> queryPageList(BimGeoIcon bimGeoIcon,
                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                   HttpServletRequest req) {
        QueryWrapper<BimGeoIcon> queryWrapper = QueryGenerator.initQueryWrapper(bimGeoIcon, req.getParameterMap());
        Page<BimGeoIcon> page = new Page<>(pageNo, pageSize);
        IPage<BimGeoIcon> pageList = bimGeoIconService.page(page, queryWrapper);
        if(CollUtil.isNotEmpty(pageList.getRecords())){
            pageList.getRecords().stream().forEach(item ->{
                if(StringUtils.isNotBlank(item.getImgUrl())) {
                    String baseUrl = sysConfigService.findByCode("SERVER_BASE_URL");
                    item.setImgUrl(baseUrl+item.getImgUrl());
                }
            });
        }
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param bimGeoIcon
     * @return
     */
    @AutoLog(value = "Icon添加")
    @ApiOperation(value = "Icon添加", notes = "Icon添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody @Valid BimGeoIcon bimGeoIcon) {
        ParamCheckUtils.check(bimGeoIcon);
        bimGeoIconService.add(bimGeoIcon);
        return Result.OK("添加成功！");
    }


    /**
     * 编辑
     *
     * @param bimGeoIcon
     * @return
     */
    @AutoLog(value = "Icon编辑")
    @ApiOperation(value = "Icon编辑", notes = "Icon编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody BimGeoIcon bimGeoIcon) {
        ParamCheckUtils.check(bimGeoIcon);
        bimGeoIconService.update(bimGeoIcon);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "bim_geo_icon-通过id删除")
    @ApiOperation(value = "Icon通过id删除", notes = "Icon通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id") Integer id) {
        bimGeoIconService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "Icon批量删除")
    //@ApiOperation(value = "Icon批量删除", notes = "Icon批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "Icon通过id查询", notes = "Icon通过id查询")
    @GetMapping(value = "/queryById")
    public Result<BimGeoIcon> queryById(@RequestParam(name = "id") String id) {
        BimGeoIcon bimGeoIcon = bimGeoIconService.getById(id);
        if (bimGeoIcon == null) {
            return Result.error("未找到对应数据", null);
        }
        if(StringUtils.isNotBlank(bimGeoIcon.getImgUrl())) {
            String baseUrl = sysConfigService.findByCode("SERVER_BASE_URL");
            bimGeoIcon.setImgUrl(baseUrl+bimGeoIcon.getImgUrl());
        }
        return Result.OK(bimGeoIcon);
    }
 
}
